<?php
class Server {
  public static function submit_system($orm) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    if ($req->php_name == '') {
      echo 'PHP is empty';
      return;
    } else if (substr($req->php_name, 0, 2) == '__') {
      echo 'Can\'t start with "__"';
      return;
    } else if (!ctype_alnum($req->php_name)) {
      echo 'Letters and/or digits only';
      return;
    } else {
      $opts = new stdClass();
      $opts->cd = 't.id != :v1 AND t.php_name = :v2';
      $opts->cdv = array(':v1' => (isset($req->id)) ? $req->id : 0, ':v2' => $req->php_name);
      $opts->sel = 'COUNT(t.id) AS total';
      $total = $orm->get('pb_sys', NULL, NULL, NULL, NULL, $opts)->d[0]->total;
      if ($total > 0) {
        echo 'PHP existed';
        return;
      }
    }
    
    if (isset($req->id)) {
      if (isset($req->__php_name) && $req->__php_name != $req->php_name) {
        if (file_exists('../' . HOME_DIR . '/' . $req->__php_name . '.php')) {
          rename('../' . HOME_DIR . '/' . $req->__php_name . '.php', '../' . HOME_DIR . '/' . $req->php_name . '.php');
        } else {
          copy(INDEX_DIST_FILE, '../' . HOME_DIR . '/' . $req->php_name . '.php');
        }
        
      }
      $orm->update('pb_sys', $req);
    } else {
      $orm->add('pb_sys', $req);
      $req->id = $orm->last_id();
      copy(INDEX_DIST_FILE, '../' . HOME_DIR . '/' . $req->php_name . '.php');
    }
    
    include '__clear.php';
    echo json_encode($req);
  }
  
  public static function update_pages($orm) {
    $reqs = json_decode(Utils::get_param('reqs', '[]'));
    
    if (count($reqs) == 1 && isset($reqs[0]->is_default) && $reqs[0]->is_default == 1) {
      $orm->exec('UPDATE pb_page SET is_default = 0 WHERE sys_id = ' . $reqs[0]->sys_id);
    }
    foreach ($reqs as &$req) {
      $orm->update('pb_page', $req);
    }
    
    include '__clear.php';
  }
  
  public static function delete_page($orm) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    Server::__delete_page($orm, $req);
    
    if ($req->is_default == 1) {
      $opts = new stdClass();
      $opts->cd = 't.sys_id = :v1';
      $opts->cdv = array(':v1' => $req->sys_id);
      $pages = $orm->get('pb_page', NULL, NULL, 1, 1, $opts)->d;
      if (count($pages) > 0) {
        $pages[0]->is_default = 1;
        $orm->update('pb_page', $pages[0]);
      }
    }
    
    $opts = new stdClass();
    $opts->cd = 't.sys_id = :v1';
    $opts->cdv = array(':v1' => $req->sys_id);
    $opts->sel = 't.id, t.parent_id, t.name, t.order, t.is_default, t.is_visible';
    $pages = $orm->get('pb_page', 't.order', 'ASC', NULL, NULL, $opts)->d;
    
    include '__clear.php';
    echo json_encode($pages);
  }
  
  public static function __delete_page($orm, $req) {
    $opts = new stdClass();
    $opts->cd = 'parent_id = :v1';
    $opts->cdv = array(':v1' => $req->id);
    $pages = $orm->get('pb_page', NULL, NULL, NULL, NULL, $opts)->d;
    foreach ($pages as &$page) {
      Server::__delete_page($orm, $page);
    }
    $orm->delete('pb_page', $req);
    $opts = new stdClass();
    $opts->cd = 'page_id = :v1';
    $opts->cdv = array(':v1' => $req->id);
    $orm->delete_by_opts('pb_plugin_instance', $opts);
  }
}
?>